﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NHibernate;
using DotNet.Common.NHibernateUtil;
using ZLCMS.Data.Model;
using NHibernate.Criterion;
using ZLCMS.Web.Wap.Code;
using ZLCMS.Business.Util;
using System.Security.Cryptography;
using System.Text;

namespace ZLCMS.Web.Wap.Shop
{
    public partial class KFList : MemberPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected string GetWebKFLink(string server)
        {
            //防止非法调用，因此没有在前端JS生成，而是在后台生成
            var timestamp = GetTimestamp();
            var nonce = GetNoncestr();
            var web_token = CurrentMemberAccount;
            var im_user_key = "a6ab9f1d9b78e098022dcce846217ff8";

            var sign_str = "nonce=" + nonce + "&timestamp=" + timestamp + "&web_token=" + web_token + "&" + im_user_key;

            var signature = GetSHA1(sign_str);

            return server + "?c_name=" + CurrentMemberAccount + "&nonce=" + nonce + "&timestamp=" + timestamp + "&web_token=" + web_token + "&signature=" + signature;
        }

        public string GetNoncestr()
        {
            return Guid.NewGuid().ToString("N");
        }


        public long GetTimestamp()
        {
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return Convert.ToInt64(ts.TotalMilliseconds);
        }

        public string GetSHA1(string mystr)
        {
            //建立SHA1对象
            SHA1 sha = new SHA1CryptoServiceProvider();

            //将mystr转换成byte[]
            ASCIIEncoding enc = new ASCIIEncoding();
            byte[] dataToHash = enc.GetBytes(mystr);

            //Hash运算
            byte[] dataHashed = sha.ComputeHash(dataToHash);

            //将运算结果转换成string
            string hash = BitConverter.ToString(dataHashed).Replace("-", "");

            return hash.ToUpper();
        }
    }
}